package com.example.aop.apsect;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author xxg
 */
@Aspect
@Component
@Slf4j
public class LogAspect {


    @Pointcut("@annotation(SysLog)")
    public  void pointCut(){

    }
    @Around("pointCut()")
    public  Object around(ProceedingJoinPoint point) throws Throwable {
        Object proceed = point.proceed();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        Parameter[] parameters = method.getParameters();
        for (Parameter parameter : parameters) {
            AnnotatedType annotatedType = parameter.getAnnotatedType();
            log.info("方法参数，type:{},name:{}",annotatedType.getType(),parameter.getName());
        }
        log.info("开始执行:{},当前时间：{}",methodName,System.currentTimeMillis());
        log.info("执行结果：{}",JSON.toJSONString(proceed));
        log.info("执行结束,当前时间：{}",System.currentTimeMillis());
        return proceed;
    }
}
